<?php
/**
 * Created by PhpStorm.
 * User: TianFeng
 * Date: 2017/12/27
 * Time: 16:27
 */
namespace app\merchant\controller;

use app\merchant\model\BankCard;
use app\merchant\model\User as TpUser;
use app\merchant\model\UserCompany;
use app\merchant\service\UserService;
use app\common\lib\MyController;
use think\Exception;
use think\Request;
use think\Cache;
use think\Session;


class  User extends MyController{

    //实名认证ajax
    public function realnameAjax(){
        try{
            $int_uid = $this->_Uid;
            $str_realname = Request::instance()->post('realname');
            $str_idcard = Request::instance()->post('idcard');
            if (!$int_uid || !$str_realname || !$str_idcard ){
                throw new \Exception('身份证，真实姓名 uid不能为空');
            }
            $res = UserService::userRealName($int_uid,$str_idcard,$str_realname);
            if($res){
                $userInfo = UserService::get_user();
                $userInfo['real_name'] = $str_realname;
                Session::set('user_info',$userInfo);
                return $this->returnData(0,'认证成功,请继续完成绑卡操作',['url'=>url('user/personal_bind_card')]);
            }
            else{
                throw new \Exception($res['msg']);
            }
        }catch (\Exception $exception){
            return $this->returnData(1,$exception->getMessage(), []);
        }
    }

    //设置支付密码 ajax
    public function setSinaPasswdAjax(){
        try{
            $int_uid = Request::instance()->post('uid');
            $str_returnUrl = Request::instance()->post('return_url','');
            if (!$int_uid){
                throw new \Exception('uid不能为空');
            }
            $res = UserService::setSinaPayPasswd($int_uid, 'PC', $str_returnUrl);

            return $res;
        }catch (\Exception $exception){
            return $this->returnData(1,$exception->getMessage(), []);
        }
    }

    public function setPayPassword(){
        try{

            $int_uid = $this->_Uid;
            $str_returnUrl = config('current_url').'merchant/index/outLogin';

            if (!$int_uid){
                throw new \Exception('uid不能为空');
            }

            $service = new UserService();
            $res = $service->setSinaPayPasswd($int_uid, 'PC', $str_returnUrl);


            if($res['code'] == 0){

                if(!empty($res['data']['redirect_url'])){

                    header('Location:'.$res['data']['redirect_url']);
                }
                else{
                    UserService::set_pay_password_status($int_uid);

                    UserService::set_user($int_uid);
                    $this->redirect('Index/message',['message'=>base64_encode('您的商户账户正在人工配置中，需要1到2个工作日内完成')]);

                }

            }
            else{
                //return parent::returnData(1,$res['msg']);//返回的是json
                $this->redirect('Index/message',['message'=>base64_encode($res['msg'])]);//重定向到页面
            }

        }catch (\Exception $exception){
            //return parent::returnData(1,$exception->getMessage());//返回的是json
            $this->redirect('Index/message',['message'=>base64_encode($exception->getMessage())]);//重定向到页面
        }
    }



    public function add_company_info(){
        $this->assign('user_id',$this->_Uid);
        return view('Login/add_company_info');
    }

    public function real_personal(){
        try {
            $int_uid = $this->_Uid;
            $str_realname = Request::instance()->post('realname');
            $str_idcard = Request::instance()->post('idcard');

            if (!$str_realname || !$str_idcard) {
                return view('Login/personal_real');
            } else {

                $service = new UserService();
                $res = $service->userRealName($int_uid, $str_realname, $str_idcard);
                if ($res) {
                    $userInfo = UserService::get_user();
                    $userInfo['real_name'] = $str_realname;
                    Session::set('user_info', $userInfo);


                    return $this->returnData(0, 'ok', ['url' => url('user/personal_bind_card')]);
                } else {
                    throw new \Exception($res['msg']);
                }

            }
        }
        catch (\Exception $e){
            return $this->returnData(1,'实名信息未通过，请填写正确的实名信息', $e->getMessage());
        }
    }

    /**
     * 完善资料
     * @return mixed
     */
    public function setCompanyInfo(){
        try{
            $company['company_name']=  get_param('company_name','str');
            $company['license_no']= get_param('license_no','str');
            $license_file_src= get_param('license_file_src','str');
            $org_no_src = get_param('org_no_src','str');
            $tax_no_src = get_param('tax_no_src','str');
            $company['license_address'] = get_param('license_address','str');
            $company['address'] = get_param('address','str');
            $company['bank_account_no'] = get_param('bank_account_no','str');
            $company['bank_branch'] = get_param('bank_branch','str');

            $company['license_expire_date'] = get_param('license_expire_date','str');
            $company['business_scope'] = get_param('business_scope','str');
            $company['telephone'] = get_param('telephone','uint');
            $company['organization_no'] = get_param('organization_no','str');
            $company['summary'] = get_param('summary','str');
            $company['website'] = get_param('website','str','');
            $company['legal_person'] = get_param('legal_person','str');
            $company['legal_person_phone'] = get_param('legal_person_phone','phone');
            $company['cert_no'] = get_param('cert_no','str');
            $front_card_src = get_param('front_card_src','str');
            $back_card_src = get_param('back_card_src','str');
            $company['bank_code'] = get_param('bank_code','str');
            $company['province'] = get_param('province','str');
            $company['city'] = get_param('city','str');
            $open_account_no_src = get_param('open_account_no_src','str');
            $image = UserCompany::getImage($license_file_src, $org_no_src,$tax_no_src,$front_card_src,$back_card_src,$open_account_no_src);
            UserService::setCompanyInfo($company, $this->_Uid,$this->_Email,$image);

            return parent::returnData(0,'申请已提交',['url'=>url('user/setpaypassword')]);
        }
        catch (\Exception $e){
            return parent::returnData(1,$e->getMessage());
        }
    }

    public function personal_bind_card(){

        return view('Login/personal_card');
    }

    public function add_card(){
        try {
            $int_uid = $this->_Uid;
            $bank_code = get_param('bank_code','str');
            $phone = get_param('phone','phone');
            $province = get_param('province','str');
            $city = get_param('city','str');
            $bank_card = get_param('bank_card','str');


            $ticket = UserService::add_card($bank_code, $phone, $province, $city, $bank_card, $int_uid);
            $data = ['bank_code' => $bank_code, 'phone' => $phone, 'province' => $province, 'city' => $city, 'bank_card' => $bank_card,
                'uid' => $int_uid, 'ticket' => $ticket];
            Cache::set($int_uid . '_ticket', json_encode($data), 300);
            return parent::returnData(0, 'ok');
        }
        catch (\Exception $e){
            return parent::returnData(1,$e->getMessage());
        }

    }

    public function add_card_advance(){
        try {
            $int_uid = $this->_Uid;
            $valid_code = get_param('valid_code','str');
            $card_data = Cache::get($int_uid . '_ticket');
            if (!$card_data || !$valid_code) {
                return parent::returnData(1, '已经过期');
            }
            $card_data = json_decode($card_data, true);


            $ret = UserService::bind_card_advance($card_data['ticket'], $valid_code);

            if ($ret['code'] == 0) {
                UserService::set_user($int_uid);
                return parent::returnData(0, '绑卡成功，去设置支付密码', ['url' => url('user/setpaypassword')]);
            } else {
                throw new \Exception($ret['msg']);
            }

        }
        catch(\Exception $e){
            return parent::returnData(1,$e->getMessage());
        }

    }

}